﻿using DevExpress.XtraEditors;
using ImageDefine;
using ProjectModule;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TableModule;


namespace GluePathModule
{
    public partial class FormGluePathUnitSetting : DevExpress.XtraEditors.XtraForm
    {
        GluePathGroup pathGroup;
        GluePathUnit pathUnit;
        public FormGluePathUnitSetting()
        {
            InitializeComponent();
        }
        public FormGluePathUnitSetting(GluePathGroup patGroupInput)
        {
            InitializeComponent();
            pathGroup = patGroupInput;
            if (patGroupInput.listGlueUnit.Count > 0)
            {
                pathUnit = patGroupInput.listGlueUnit[0];
            }
            else
            {
                GluePathUnit newUnit = new GluePathUnit();
                newUnit.Name = "GluePathUnit1";
                pathGroup.listGlueUnit.Add(newUnit);
                pathUnit = newUnit;

            }
            if (pathUnit.listContinueItems.Count > 0)
            {
                foreach (var item in pathUnit.listContinueItems)
                {
                    if (item.Select)
                    {
                        propertyGridItem.SelectedObject = item;
                        break;
                    }
                }
                
            }
        }

        private void FormGluePathUnitSetting_Load(object sender, EventArgs e)
        {
            drawerControl1.Init(pathGroup, pathUnit, propertyGridItem);
            drawerControl1.DrawPathGlueGroup();
            propertyGridUnit.SelectedObject = pathUnit;
            drawerControl1.SetToAutoFill();
        }

        private void propertyGridUnit_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            drawerControl1.DrawPathGlueGroup();
        }

        private void buttonOffYAdd_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否真的偏移路径", "警告", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                return;
            }
            double dOffsetValue = 0;
            bool bParse = double.TryParse(comboBoxEditMoveDis.Text, out dOffsetValue);
            if (bParse == false)
            {
                return;
            }
            for (int i = pathUnit.listContinueItems.Count - 1; i > -1; i--)
            {
                if (pathUnit.listContinueItems[i].Select)
                {
                    pathUnit.listContinueItems[i].OffSetPos(0, dOffsetValue, 0);
                }
            }

            drawerControl1.DrawPathGlueGroup();
            propertyGridItem.Refresh();
        }

        private void buttonOffXAdd_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否真的偏移路径", "警告", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                return;
            }
            double dOffsetValue = 0;
            bool bParse = double.TryParse(comboBoxEditMoveDis.Text, out dOffsetValue);
            if (bParse == false)
            {
                return;
            }
            for (int i = pathUnit.listContinueItems.Count - 1; i > -1; i--)
            {
                if (pathUnit.listContinueItems[i].Select)
                {
                    pathUnit.listContinueItems[i].OffSetPos(dOffsetValue, 0, 0);
                }
            }

            drawerControl1.DrawPathGlueGroup();
            propertyGridItem.Refresh();
        }

        private void buttonOffYSub_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否真的偏移路径", "警告", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                return;
            }
            double dOffsetValue = 0;
            bool bParse = double.TryParse(comboBoxEditMoveDis.Text, out dOffsetValue);
            if (bParse == false)
            {
                return;
            }
            for (int i = pathUnit.listContinueItems.Count - 1; i > -1; i--)
            {
                if (pathUnit.listContinueItems[i].Select)
                {
                    pathUnit.listContinueItems[i].OffSetPos(0, -dOffsetValue, 0);
                }
            }

            drawerControl1.DrawPathGlueGroup();
            propertyGridItem.Refresh();
        }

        private void buttonOffXSub_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否真的偏移路径", "警告", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                return;
            }
            double dOffsetValue = 0;
            bool bParse = double.TryParse(comboBoxEditMoveDis.Text, out dOffsetValue);
            if (bParse == false)
            {
                return;
            }
            for (int i = pathUnit.listContinueItems.Count - 1; i > -1; i--)
            {
                if (pathUnit.listContinueItems[i].Select)
                {
                    pathUnit.listContinueItems[i].OffSetPos(-dOffsetValue, 0, 0);
                }
            }

            drawerControl1.DrawPathGlueGroup();
            propertyGridItem.Refresh();
        }

        private void button统一高度_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否真的统一高度路径", "警告", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                return;
            }
            double dOffsetValue = 0;
            bool bParse = double.TryParse(textBoxZPos.Text, out dOffsetValue);
            if (bParse == false)
            {
                return;
            }
            for (int i = pathUnit.listContinueItems.Count - 1; i > -1; i--)
            {
                if (pathUnit.listContinueItems[i].Select)
                {
                    pathUnit.listContinueItems[i].SetZPos(dOffsetValue);
                }
            }

            drawerControl1.DrawPathGlueGroup();
            propertyGridItem.Refresh();
        }
        List<MotionItemBase> MotionItems;
        private void button生成运动轨迹_Click(object sender, EventArgs e)
        {
            TableDataPosition OffSet = TableManager.tableRunner.GetPosition("偏移设定");
            MotionItems = pathGroup.GetMotionList(OffSet.dZPos);
            UpdateMotionPath();
        }
        void UpdateMotionPath()
        {
            dataGridViewMotionItems.Rows.Clear();
            for (int i = 0; i < MotionItems.Count; i++)
            {
                DataGridViewRow rowAdd = new DataGridViewRow();

                DataGridViewTextBoxCell noCell = new DataGridViewTextBoxCell();
                noCell.Value = i.ToString();

                DataGridViewTextBoxCell nameCell = new DataGridViewTextBoxCell();
                nameCell.Value = MotionItems[i].GetType().ToString();

                DataGridViewTextBoxCell remarkCell = new DataGridViewTextBoxCell();
                remarkCell.Value = MotionItems[i].ToString();

                rowAdd.Cells.Add(noCell);
                rowAdd.Cells.Add(nameCell);
                rowAdd.Cells.Add(remarkCell);

                dataGridViewMotionItems.Rows.Add(rowAdd);
            }
        }

        private void button统一角度_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否真的统一角度路径", "警告", MessageBoxButtons.YesNo) != DialogResult.Yes)
            {
                return;
            }
            double dOffsetValue = 0;
            bool bParse = double.TryParse(textBoxUPos.Text, out dOffsetValue);
            if (bParse == false)
            {
                return;
            }
            for (int i = pathUnit.listContinueItems.Count - 1; i > -1; i--)
            {
                if (pathUnit.listContinueItems[i].Select)
                {
                    pathUnit.listContinueItems[i].SetUPos(dOffsetValue);
                }
            }
            propertyGridItem.Refresh();
        }

        private void FormGluePathUnitSetting_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (drawerControl1.bitmapBackGroup != null)
            {
                if (drawerControl1.bitmapBackGroup.bitmap != null)
                {
                    drawerControl1.bitmapBackGroup.bitmap.Dispose();
                    drawerControl1.bitmapBackGroup.bitmap = null;
                    drawerControl1.bitmapBackGroup = null;
                }
            }
        }

        private void simpleButtonRunRegeditFile_Click(object sender, EventArgs e)
        {
            double dZoffSet = TableModule.TableManager.tableRunner.GetPosition("定位区域离皮带高度").dZPos;
            ImageCalibModule.ImageCalibManager.ImageCalibHandeye.SetZOffSet(dZoffSet);
            ImageTask imageTask = ImageTaskModule.ImageTaskManager.ImageTask("ProductDetect");
            imageTask.RunRegeditFileImage(smartDisplayControl1);
            if (imageTask.bResultOK)
            {
                pathUnit.StandardLocalX = imageTask.dResultX;
                pathUnit.StandardLocalY = imageTask.dResultY;
                pathUnit.StandardLocalU = imageTask.dResultA;
                propertyGridUnit.Refresh();
            }
        }
    }
}